<template>
  <div>
    <a-modal v-model:open="open" title="更新资源" @ok="handleOk">
      <EditForm :hidePassword="false" ref="rForm"></EditForm>
    </a-modal>
  </div>
</template>
<script lang="ts" setup>
import { ref, defineEmits, nextTick } from 'vue'
import EditForm from './edit-form.vue'
import { updatePermissionApi, type UpdatePermissionDto } from '@/api/permission.api.ts'

const emits = defineEmits(['editOk'])

const open = ref<boolean>(false)
const Record = ref()

const showModal = (record: UpdatePermissionDto) => {
  open.value = true
  Record.value = { ...record }
  nextTick(() => {
    rForm.value?.setFormState(Record.value)
  })
}

const rForm = ref()
const handleOk = () => {
  rForm.value.submit().then((res: UpdatePermissionDto) => {
    updatePermissionApi(Record.value.id, res).then(() => {
      open.value = false
      emits('editOk')
    })
  })
}

defineOptions({
  name: 'EditDictModal',
})
defineExpose({
  showModal,
})
</script>
